package cn.tedu.smartos.seniorcourse.service.impl;

import cn.tedu.smartos.base.exception.ServiceException;
import cn.tedu.smartos.seniorcourse.mapper.CourseMapper;
import cn.tedu.smartos.seniorcourse.pojo.dto.*;
import cn.tedu.smartos.seniorcourse.pojo.entity.Course;
import cn.tedu.smartos.seniorcourse.pojo.entity.CourseCategory;
import cn.tedu.smartos.seniorcourse.pojo.entity.CourseLesson;
import cn.tedu.smartos.seniorcourse.pojo.entity.LearningRecord;
import cn.tedu.smartos.seniorcourse.pojo.vo.CourseCategoryVO;
import cn.tedu.smartos.seniorcourse.pojo.vo.CourseLessonVO;
import cn.tedu.smartos.seniorcourse.pojo.vo.CourseVO;
import cn.tedu.smartos.seniorcourse.pojo.vo.LearningRecordVO;
import cn.tedu.smartos.seniorcourse.service.CourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 获取所有课程列表
     *
     * @param pageNum  页码
     * @param pageSize 每页记录数
     * @return 课程列表
     */
    @Override
    public PageInfo<CourseVO> list(Integer categoryId,Integer pageNum, Integer pageSize) {
        log.debug("开始处理【课程列表】的请求,参数{},{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<CourseVO> courseVOS = courseMapper.list(categoryId);
        return new PageInfo<>(courseVOS);
    }
    /**
     * 根据课程ID查询课程详情
     *
     * @param courseId 课程ID
     * @return 课程详情
     */
    @Override
    public CourseVO detail(Long courseId) {
        log.debug("开始处理【根据课程ID查询课程详情】的业务，参数：{}", courseId);
        if (courseId == null){
            throw new ServiceException("参数courseId不能为空");
        }
        CourseVO courseVO = courseMapper.detail(courseId);
        return courseVO;
    }
    /**
     * 添加课程
     *
     * @param courseAddParam 添加课程参数
     */
    @Override
    public void add(CourseAddParam courseAddParam) {
        log.debug("开始处理【添加课程】的请求,参数{}",courseAddParam);
        Course course = new Course();
        BeanUtils.copyProperties(courseAddParam,course);
        course.setCreateTime(new Date());
        course.setUpdateTime(new Date());
        Integer rows = courseMapper.add(course);
        if (rows != 1){
            throw new ServiceException("添加失败");
        }
    }
    /**
     * 删除课程
     * @param courseIds 课程ID
     */
    @Override
    public void delete(List<Long> courseIds) {
        log.debug("开始处理【删除课程】的请求,参数{}",courseIds);
        if (CollectionUtils.isEmpty(courseIds)){
            throw new ServiceException("参数courseIds不能为空");
        }
        Integer rows = courseMapper.delete(courseIds);
        if (rows == 0){
            throw new ServiceException("删除课程失败");
        }
        if (rows != courseIds.size()){
            throw new ServiceException("部分课程删除失败");
        }
    }
    /**
     * 修改课程
     * @param courseUpdateParam 修改课程参数
     */
    @Override
    public void update(CourseUpdateParam courseUpdateParam) {
        log.debug("开始处理【修改课程】的请求,参数{}",courseUpdateParam);
        Course course = new Course();
        BeanUtils.copyProperties(courseUpdateParam,course);
        course.setUpdateTime(new Date());
        Integer rows = courseMapper.update(course);
        if (rows == 0) {
            throw new ServiceException("修改课程失败");
        }
    }

//——————————————————————————————————————————————————————————————————————————

    /**
     * 获取课程分类列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<CourseCategoryVO> listCategory(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【获取课程分类列表】的请求,参数{},{}",pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<CourseCategoryVO> courseCategoryVOS = courseMapper.listCategory();
        return new PageInfo<>(courseCategoryVOS);
    }
    /**
     * 获取课程分类详情
     * @param categoryId 活动ID
     * @return 活动详情
     */
    @Override
    public CourseCategoryVO detailCategory(Long categoryId) {
        log.debug("开始处理【获取课程分类详情】的业务，参数：{}", categoryId);
        if (categoryId == null){
            throw new ServiceException("课程分类ID不能为空");
        }
        CourseCategoryVO courseCategory = courseMapper.detailCategory(categoryId);
        return courseCategory;
    }
    /**
     * 添加课程分类
     * @param courseCategoryAddParam 添加课程分类参数
     */
    @Override
    public void addCategory(CourseCategoryAddParam courseCategoryAddParam) {
        log.debug("开始处理【添加课程分类】的请求,参数{}",courseCategoryAddParam);
        CourseCategory courseCategory = new CourseCategory();
        BeanUtils.copyProperties(courseCategoryAddParam,courseCategory);
        courseCategory.setCreateTime(new Date());
        Integer rows = courseMapper.addCategory(courseCategory);
        if (rows != 1){
            throw new ServiceException("添加课程分类失败");
        }
    }
    /**
     * 删除课程分类
     * @param categoryIds 分类ID集合
     */
    @Override
    public void deleteCategory(List<Long> categoryIds) {
        log.debug("开始处理【删除课程分类】的请求,参数{}",categoryIds);
        if (CollectionUtils.isEmpty(categoryIds)){
            throw new ServiceException("参数categoryIds不能为空");
        }
        Integer rows = courseMapper.deleteCategory(categoryIds);
        if (rows == 0){
            throw new ServiceException("删除出错");
        }
        if (rows != categoryIds.size()){
            throw new ServiceException("部分分类删除失败");
        }
    }

//————————————————————————————————————————————————————————————————————————————————

    /**
     * 获取课程章节列表
     * @param courseId 课程ID
     * @param pageNum  页码
     * @param pageSize 每页记录数
     * @return 课程章节列表
     */
    @Override
    public PageInfo<CourseLessonVO> listLessons(Long courseId, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【课程课时列表】的请求,参数{},{},{}",courseId,pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<CourseLessonVO> courseLessonVOS = courseMapper.listLessons(courseId);
        return new PageInfo<>(courseLessonVOS);
    }
    /**
     * 课程课时详情
     * @param lessonId 课时ID
     * @return 课时详情
     */
    @Override
    public CourseLessonVO detailLesson(Long lessonId) {
        log.debug("开始处理【课程课时详情】的业务，参数：{}", lessonId);
        if (lessonId == null){
            throw new ServiceException("课时ID不能为空");
        }
        CourseLessonVO courseLessonVO = courseMapper.detailLesson(lessonId);
        return courseLessonVO;
    }
    /**
     * 添加课程课时
     * @param courseLessonAddParam 添加课时参数
     */
    @Override
    public void addLesson(CourseLessonAddParam courseLessonAddParam) {
        log.debug("开始处理【添加课程课时】的请求,参数{}",courseLessonAddParam);
        CourseLesson courseLesson = new CourseLesson();
        BeanUtils.copyProperties(courseLessonAddParam,courseLesson);
        courseLesson.setCreateTime(new Date());
        courseLesson.setUpdateTime(new Date());
        Integer rows = courseMapper.addLesson(courseLesson);
        if (rows != 1){
            throw new ServiceException("添加失败");
        }
    }
    /**
     * 删除课程课时
     * @param lessonIds 课时ID集合
     */
    @Override
    public void deleteLesson(List<Long> lessonIds) {
        log.debug("开始处理【删除课程课时】的请求,参数{}",lessonIds);
        if (CollectionUtils.isEmpty(lessonIds)){
            throw new ServiceException("参数lessonIds不能为空");
        }
        Integer rows = courseMapper.deleteLesson(lessonIds);
        if (rows == 0){
            throw new ServiceException("删除出错");
        }
        if (rows != lessonIds.size()){
            throw new ServiceException("部分课时删除失败");
        }
    }
    /**
     * 修改课时
     * @param courseLessonUpdateParam 修改课时参数
     */
    @Override
    public void updateLesson(CourseLessonUpdateParam courseLessonUpdateParam) {
        log.debug("开始处理【修改课时】的请求,参数{}",courseLessonUpdateParam);
        CourseLesson courseLesson = new CourseLesson();
        BeanUtils.copyProperties(courseLessonUpdateParam,courseLesson);
        courseLesson.setUpdateTime(new Date());
        Integer rows = courseMapper.updateLesson(courseLesson);
        if (rows != 1){
            throw new ServiceException("修改课时失败");
        }
    }

//————————————————————————————————————————————————————————————————————————————————

    /**
     * 添加学习记录
     * @param learningRecordAddParam 添加学习记录参数
     */
    @Override
    public void addRecord(LearningRecordAddParam learningRecordAddParam) {
        log.debug("开始处理【添加学习记录】的请求,参数{}",learningRecordAddParam);
        LearningRecord learningRecord = new LearningRecord();
        BeanUtils.copyProperties(learningRecordAddParam,learningRecord);
        learningRecord.setCreateTime(new Date());
        Integer rows = courseMapper.addRecord(learningRecord);
        if (rows != 1) {
            throw new ServiceException("添加学习记录失败");
        }
    }
    /**
     * 学习记录列表
     * @param elderlyUserId 老人用户ID
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return PageInfo<LearningRecordVO>
     */
    @Override
    public PageInfo<LearningRecordVO> listRecords(Long elderlyUserId, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【课程学习记录列表】的请求,参数{},{},{}",elderlyUserId,pageNum,pageSize);
        if (elderlyUserId == null){
            throw new ServiceException("老人用户ID不能为空");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<LearningRecordVO> list = courseMapper.listRecords(elderlyUserId);
        return new PageInfo<>(list);
    }
    /**
     * 获取课程学习记录详情
     * @param recordId 课程学习记录id
     * @return LearningRecordVO
     */
    @Override
    public LearningRecordVO detailRecord(Long recordId) {
        log.debug("开始处理【获取课程学习记录详情】的业务，参数：{}", recordId);
        if (recordId == null){
            throw new ServiceException("课程学习记录ID不能为空");
        }
        LearningRecordVO learningRecordVO = courseMapper.detailRecord(recordId);
        return learningRecordVO;
    }
    /**
     * 删除课程学习记录
     * @param recordIds 学习记录ID集合
     */
    @Override
    public void deleteRecord(List<Long> recordIds) {
        log.debug("开始处理【删除课程学习记录】的请求,参数{}",recordIds);
        if (CollectionUtils.isEmpty(recordIds)){
            throw new ServiceException("参数recordIds不能为空");
        }
        Integer rows = courseMapper.deleteRecord(recordIds);
        if (rows == 0){
            throw new ServiceException("删除出错");
        }
        if (rows != recordIds.size()){
            throw new ServiceException("部分记录删除失败");
        }
    }
}
